<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Model 流式与非流式请求测试</title>
    <script src="https://unpkg.com/vue@3"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        #output, #stream-output {
            white-space: pre-wrap;
            border: 1px solid #ccc;
            padding: 10px;
            width: 100%;
            height: 200px;
            overflow-y: auto;
        }

        .container {
            margin: 20px;
        }

        input {
            padding: 5px;
            width: 300px;
        }

        button {
            margin-left: 10px;
            padding: 5px 15px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div id="app">
    <h1>AI Model 请求测试</h1>

    <div class="container">
        <input v-model="input" placeholder="请输入您的问题">
        <button @click="sendNonStreamRequest">发送非流式请求</button>
        <button @click="sendStreamRequest">发送流式请求</button>
    </div>

    <div class="container">
        <h2>非流式响应</h2>
        <div id="output">
            <pre>{{ response }}</pre>
        </div>
    </div>

    <div class="container">
        <h2>流式响应</h2>
        <div id="stream-output">
            <pre>{{ streamResponse }}</pre>
        </div>
    </div>
</div>

<script>
    const app = Vue.createApp({
        data() {
            return {
                input: '', // 用户输入
                response: null, // 非流式响应内容
                streamResponse: '' // 流式响应内容
            };
        },
        methods: {
            // 非流式请求
            async sendNonStreamRequest() {
                try {
                    // 调用非流式接口
                    const res = await axios.get('/test/xf/ai', {
                        params: { input: this.input }
                    });
                    this.response = JSON.stringify(res.data, null, 2); // 格式化显示
                } catch (error) {
                    console.error('非流式请求失败:', error);
                    this.response = '请求失败，请检查控制台日志';
                }
            },

            // 流式请求
            sendStreamRequest() {
                this.streamResponse = ''; // 清空上次的流式响应
                const eventSource = new EventSource(`/test/xf/stream?input=${encodeURIComponent(this.input)}`);

                // 接收数据块
                eventSource.onmessage = (event) => {
                    console.log('收到流式数据:', event.data);
                    this.streamResponse += event.data + '\n'; // 累积响应内容
                };

                // 连接错误处理
                eventSource.onerror = (error) => {
                    console.error('流式请求出错:', error);
                    this.streamResponse += '\n连接已关闭\n';
                    eventSource.close();
                };

                // 连接打开
                eventSource.onopen = () => {
                    console.log('流式请求连接成功');
                };
            }
        }
    });

    app.mount('#app');
</script>
</body>
</html>
